/**
* Class to establish communication with Servlet DB
*
* Project: idbcrown
* Author:  TM
* Date:    03/01/2012
*
* History
* ------------------
* 03/01/2012 - TM - Initial version.
*/
 
 
package comm;

import java.util.*;
import java.io.*;
import java.net.*;
import java.sql.*;

import Application.*;

public class servletDBComm implements constantsInterface
{
   /** Communication */
   protected String protocol;
   protected String host;
   protected int    port;
   protected String uri;
   /** Singleton support */
   protected static servletDBComm comm;
   /** Application support */
   protected static libApplication appSupport;
   
   
   /** Constructor for singleton without iniFile */
   protected servletDBComm()
   {
     appSupport = libApplication.getInstance();	
     protocol   = appSupport.getConfigValue(PROTOCOL);
     host       = appSupport.getConfigValue(HOST);
     port       = appSupport.getConfigInt  (PORT, 8080);
     uri        = appSupport.getConfigValue(URI);
   }

   /** Singleton */
   public static servletDBComm getInstance()
   {
     if (comm == null)
       comm = new servletDBComm();
       
     return comm;
   }

  /** Current time stamp from service */
  public String post_get(String parameters)
  {
    String result = "";
    String line   = null;
    try
    {
      //URL u = new URL("http", tsHost, tsPort.intValue(), tsURI);
      String         postParams = DB_PREF_ACTION + parameters;
      URL            u          = new URL(protocol, host, port, uri + postParams);
      System.out.println("Send:"+u);
      URLConnection  urlCnx     = u.openConnection();
      InputStream    is         = urlCnx.getInputStream();
      BufferedReader d          = new BufferedReader(new InputStreamReader(is, "UTF-8"));
      
      while ((line = d.readLine()) != null)
          result += " " + line;
      
      result = result.trim();
      
      System.out.println("Response:"+result);
      
    }
    catch(Exception ex)
    {
      appSupport.showErrorMsg(ex);
    }
    
    return result;
  }
  
  //Static support
  
   /** Concatenates parameters to be sent encoded by URL */
   public static String getFormattedParameters(String actionParam, String[] names, String[] values) throws Exception
   {
     String result = null;
   
     if (names.length != values.length)
       return result;
   
     result = actionParam + "&";
   
     for (int i=0; i<names.length; i++)
       result += names[i] + "=" + URLEncoder.encode(values[i].trim(), "UTF-8") + "&";
   
     return result;  

   }
 
  /** Concatenates parameters to be sent */
  public static String getFormedParameters(String actionParam, String[] names, String[] values)
  {
     String result = null;
     
     if (names.length != values.length)
       return result;
     
     result = actionParam + "&";
     
     for (int i=0; i<names.length; i++)
       result += names[i] + "=" + values[i] + "&";
     
     return result;  
  }

  /** Says if result contains error message */
  public static boolean isError(String msg)
  {
     return msg.startsWith(ERROR);
  }
}